﻿/**
 * 生产相关基础方法
 * Dai 2022-12-26
 * **/

/**
  获取生产工序数据(树状格式)
  **/
function GxData(proType,plCode) {
    if (proType == "") {
        layer.msg("请选择品类"); return;
    }
    $.ajax({
        url: "../ProductSet/GongXuNavigation",
        data: { proType: proType, plCode: plCode },
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.Result) {
                loadTree(data.Data,"gxTree");
                GetProGxTree(proType,plCode);
            } else {
                layer.msg(data.Message);
            }
        }
    });
}
/**
 * 班组成员
 **/
function GroupUserTree(groupCode,treeId) {
    if (treeId == undefined || treeId == "") {
        treeId = "TreeId";
    }
    $.ajax({
        url: "../ProductSet/GroupUserTree",
        data: { groupCode: groupCode },
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.Result) {
                loadTree(data.Data, treeId);
                GetGroupUserTree(groupCode, treeId);
            } else {
                layer.msg(data.Message);
            }
        }
    });
}
/**
 * 根据生产班组编码所有人员默认选中
 **/
function GetGroupUserTree(groupCode,treeId) {
    //if (groupCode == "") {
    //    layer.msg("班组编码获取失败"); return;
    //}
    if (treeId == undefined || treeId == "") {
        treeId = "TreeId";
    }
    url = "../Plan/GetGroupUser";
    layui.use(['form', 'layedit', 'laydate', 'tree', 'util'], function () {
        var tree = layui.tree;
        tree.setChecked(treeId, [groupCode]);
    })
}
/**
 * 
 * 加载数据树结构
 **/
function loadTree(data, treeId) {
    if (treeId == undefined || treeId == "") {
        treeId = "TreeId";
    }
    var tree = layui.tree;
    //开启节点操作图标
    tree.render({
          elem: '#treeNav'
        , data: data
        , showCheckbox: true
        , id: treeId
        , click: function (obj) {

        }
    });
}

/**
 * proType：数据大类编码
 * dType:数据类型(pro/plan)
 **/
function GetProGxTree(proType,plCode) {
    var url = "";
    if (plCode == undefined || plCode =="undefined"  || plCode == "") {
        url = "../ProductSet/GetGongXuByProType";
    } else {
        url = "../Plan/GetPlanGx";
    }
    //url = "../ProductSet/GetGongXuByProType";
    $.ajax({
        url: url,
        data: { proType: proType, plCode: plCode, order: $("input[ name='orderCode']").val(),rowNo: $("input[ name='rowNo']").val(), },
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.Result) {
                layui.use(['form', 'layedit', 'laydate', 'tree', 'util'], function () {
                    var tree = layui.tree;
                   // var checkData = tree.getChecked('gxTree');
                    var select = new Array();
                    for (var i = 0; i < data.Data.length; i++) {
                        select.push(data.Data[i].gxCode);
                    }
                    //alert(JSON.stringify(data.Data));
                    tree.setChecked('gxTree', select);
                })
            }
        }
    });
}

/**
  获取选中节点的id
  **/
function getChecked_list(data) {
    var id = "";
    $.each(data, function (index, item) {
        if (id != "") {
            id = id + "," + item.id;
        }
        else {
            id = item.id;
        }
        var i = getChecked_list(item.children);
        if (i != "") {
            id = id + "," + i;
        }
    });
    return id;
}